import { getrole } from "../api/role";
import router from "../router";
import storage from "./storage";

export const Igetrole = () => {
  return new Promise(async (resolve, reject) => {
    try {
      let res = await getrole() as any;
      if (res) {
        let fres =
          res &&
          res.data.filter((e: any) => e.disabled != true)
            .map((e: any) => e.roles)
            .flat()
            .map((es: any) => es._id);

        let rres =
          res &&
          res.data.filter((e: any) => e.disabled != true)
            .map((e: any) => e.btnroles)
            .flat()
            .map((es: any) => es);
        resolve({ pagerole: fres, btnrole: rres })

      }
    } catch (error) {
      storage.clear();
      router.replace("/login");
      reject(error);
    }
  });
};
